<?php
namespace DRY\Module;

/**
 * DonReY Framework 2012 :: Advanced debugger module.
 * Allows debugging info to be sent to something else than the screen ( to file, to FirePHP, to webserver logs, to network communication? )
 *
 * @author Wavetrex <wavetrex@gmail.com>
 * @link http://www.donrey.net/Documentation/Structure/Modules/debug.html
 * @license [GPL v3] http://www.gnu.org/licenses/gpl-3.0.html
 * @version 1.0.dev
 * @package DonReY
 * @subpackage Module.Debug
 */
class Debug extends base
{
	const
		Autoload_Directory = 'debug',
		Autoload_Prefix = 'DRY\Debug\Driver',
		Autoload_Suffix = 'debugger'
		;

	/**
	 * The debug-output driver
	 * @var Debug_Driver_base
	 */
	private $driver;

	/**
	 * Module Constructor
	 * @param string $alias
	 * @param SimpleXMLElement $config
	 */
	public function __construct($alias, $config)
	{
		parent::__construct($alias, $config);

		// ... but do not necessarily instance it
		if(!(isset($this-> CFG-> driver['load']) && $this-> CFG-> driver['load'] == 'lazy'))
			$this-> load_Driver();
	}

	/**
	 * Creates the driver object
	 */
	private function load_Driver()
	{
		$debug_class = self::Autoload_Prefix.'_'.($this-> CFG-> driver?:'FirePHP'); // defaults to FirePHP since it's the most useful
		$this-> driver = new $debug_class($this-> CFG);
	}

	/**
	 * Function pass-through to debug driver
	 *
	 * @param string $fname
	 * @param array $args
	 * @return mixed return from the debug driver
	 */
	public function __call($fname, $args)
	{
		// lazy load
		if(!$this-> driver)
			$this-> load_Driver();

		// TODO: A way to check if the function actually exists
		return call_user_func_array(array($this-> driver, $fname), $args);
	}

	/**
	 * Variable pass-through to debug driver - Read
	 *
	 * @param string $name
	 */
	public function __get($name)
	{
		// lazy load
		if(!$this-> driver)
			$this-> load_Driver();

		return $this-> driver-> $name;
	}

	/**
	 * Variable pass-through to debug driver - Write
	 *
	 * @param string $name
	 */
	public function __set($name, $value)
	{
		// lazy load
		if(!$this-> driver)
			$this-> load_Driver();

		$this-> driver-> $name = $value;
	}
/*
	public function get_Timedif()
	{
		$t = microtime(true);
		(... do stuff ...)
		$d = - $t + ($t = microtime(true));
		(... do more stuff ...)
		$d = - $t + ($t = microtime(true));
		(... etc ...)
	}
*/
}

namespace DRY\Debug;

/**
 * Debug driver required functions
 *
 * @author Wavetrex <wavetrex@gmail.com>
 * @link http://www.donrey.net/Documentation/Structure/Modules/debug.html
 * @license [GPL v3] http://www.gnu.org/licenses/gpl-3.0.html
 * @version 1.0.dev
 * @package DonReY
 * @subpackage Module.Debug
 */
interface Driver_base
{
    /**
     * Log a debugging object (coming from Water)
     *
     * @param object $output_element @see \DRY\Water::$output
     */
    public function output($output_element, $time = null);
}